﻿using System;
using System.Collections.Generic;
using NEasy.Ldap;
using NEasy.Ldap.Entity;
using NUnit.Framework;

// ReSharper disable InconsistentNaming
namespace NEasyLdap.Tests.Documentation
{
    public class RepositoryUsage
    {
        [Test()]
        public void Update_Entity()
        {
            ILdapRepository repository = new LdapRepository(new LdapDomainInfo("mentzel.org", "CN=Users,DC=Mentzel,DC=org"));

            AdEntityPrincipal user = repository.Find(new AdEntityPrincipal() { SAMAccountName = "Administrator" });

            user.TelephoneNumber = "123/456-789";
            user.Mail = "foo@bar.de";

            repository.Update(user);
        }

        [Test()]
        public void SaveNew_Entity()
        {
    ILdapRepository repository = new LdapRepository(new LdapDomainInfo("mentzel.org", "CN=Users,DC=Mentzel,DC=org", "Administrator", ""));

    var user = new LdapUserBase()
                    {
                        CanonicalName = "Test01",
                        SAMAccountName = "Test01",
                        DistinguishedName = "Thomas User",
                        ObjectClass = "user"
                    };

    repository.SaveNew(user);

    repository.SetPassword(user, "newPwd");
        }

        [Test()]
        public void Get_Groups()
        {
            ILdapRepository repository = new LdapRepository(new LdapDomainInfo("mentzel.org", "CN=Users,DC=Mentzel,DC=org"));

            AdEntityPrincipal user = repository.Find(new AdEntityPrincipal() { SAMAccountName = "Administrator" });

            IEnumerable<LdapGroupBase> groups = user.GetGroups();

            foreach (var ldapGroupBase in groups)
                Console.WriteLine(ldapGroupBase.CanonicalName);
        }
    }
}
